<?php
// 接口-库存余额
namespace interface\pda;

use control\inventoryBalanceClass;

class inventoryClass extends index
{
	private const        table = 'inventory_record';

	public function search()
	{
		$this->code         = 1;
		$this->resp['data'] = [
			'orderNo' => '物料条码',
			'id'      => '物料id',
		];
	}

	/**
	 * 详情
	 *
	 * @return void
	 */
	public function detail(): void
	{
		$where = "";
		$and   = search([
			'='    => [
				'm.coid'    => self::$coid,
				'm.barcode' => $this->post('code'),
				'm.id'      => $this->post('id'),
			],
			'like' => [
				'm.name' => $this->post('name'),
			],
		]);
		if (self::isNull($and)) {
			$this->resp['warn'] = '条件不能为空';
		} else {
			$info = query('material as m', $where . $and, 'm.*');
			if (empty($info['id'])) {
				$this->resp['warn'] = '未找到此物料信息';
			} else {
				$total       = inventoryBalanceClass::inventoryNum($info['id']);
				$unitPrice   = inventoryBalanceClass::unitPrice($info['id']);
				$unitOneNum  = empty((float)$info['unit_one_num']) ? '-' : round($total / $info['unit_one_num'], 2);
				$unitTwoNum  = empty((float)$info['unit_two_num']) ? '-' : round($total / $info['unit_two_num'], 2);
				$fieldArr    = self::resolveField([
					'field' => 'warehouseId, materialId, sum(num) as num ',
					'group' => 'warehouseId',
				]);
				$result      = find([
					'field' => $fieldArr['field'],
					'table' => self::table,
					'where' => generateWhere([
							'materialId' => $info['id'],
							'coid'          => self::$coid,
						]) . $fieldArr['group'] . " ORDER by num",
				]);
				$warehouseTr = [];
				foreach ($result['array'] as $array) {
					$unitNum       = $array['num'];
					$unitOneNum    = empty((float)$info['unit_one_num']) ? '' : round($unitNum / $info['unit_one_num'], 2);
					$unitTwoNum    = empty((float)$info['unit_two_num']) ? '' : round($unitNum / $info['unit_two_num'], 2);
					$warehouse     = query('warehouse', " id = '$array[warehouseId]' ");
					$warehouseTr[] = [
						'name'       => $warehouse['name'],
						'unit'       => $info['unit'],
						'num'        => $array['num'],
						'unitOne'    => $info['unit_one'] ?: '-',
						'unitOneNum' => $unitOneNum,
						'unitTwo'    => $info['unit_two'] ?: '-',
						'unitTwoNum' => $unitTwoNum,
					];
				}
				$this->resp['data'] = [
					'info'      => [
						"type"       => $info['typeName'],
						"code"       => $info['code'],
						"name"       => $info['name'],
						"mode"       => $info['model'],
						"unit"       => $info['unit'],
						"unitOne"    => $info['unit_one'] ?: '-',
						"unitTwo"    => $info['unit_two'] ?: '-',
						"total"      => $total,
						"price"      => $unitPrice,
						"money"      => round($total * $unitPrice, 2),
						"unitOneNum" => $unitOneNum,
						"unitTwoNum" => $unitTwoNum,
					],
					'warehouse' => $warehouseTr,
				];
				$this->code         = 1;
			}
		}
	}

	/**
	 * 查找物料
	 *
	 * @return void
	 */
	public function material(): void
	{
		$waid = $this->post('waid') ?: '';
		if (self::isNull($waid)) {
			$this->resp['warn'] = '仓库不能为空';
		} else {
			$where  = 'm.id = r.materialId ';
			$where  .= search([
				"="    => [
					'm.coid' => self::$coid,
				],
				'like' => [
					'm.code' => $this->post('code'),
					'm.name' => $this->post('name'),
				],
			]);
			$result = find([
				'table' => 'material as m, inventory_record as r',
				'where' => $where,
				'field' => 'm.*',
			]);
			if (empty($result['array'])) {
				$this->resp['warn'] = '当前账套或仓库不存在该物料，请确认';
				return;
			}
			$item = [];
			foreach ($result['array'] as $material) {
				$inventoryNum             = inventoryBalanceClass::inventoryNum($material['id'], $waid);
				$item[$material['id']] = [
					'id'           => $material['id'],
					'coid'         => $material['coid'],
					'materialId'   => $material['id'],
					'code'         => $material['code'],
					'name'         => $material['name'],
					'model'        => $material['model'],
					'skuCode'      => $material['barcode'],
					'unit'         => $material['unit'],
					'unitOption'   => inventoryBalanceClass::materialUnit($material['id']),
					'inventoryNum' => $inventoryNum,
				];
			}
			$this->resp['data'] = [
				'result' => array_values($item),
			];
			$this->code         = 1;
		}
	}


	/**
	 * 获取分类名称
	 *
	 * @param $id
	 * @return mixed
	 */
	public static function getTypeName($id): mixed
	{
		$type = query('material_type', "id = '$id'");
		if (!empty($type['pid'])) {
			$result = self::getTypeName($type['pid']);
			$name   = $result . '->' . $type['name'];
		} else {
			$name = $type['name'] ?? null;
		}
		return $name;
	}

}
